package com.qf.controller;

import com.qf.pojo.Msg;
import com.qf.pojo.User;
import com.qf.service.UserService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired(required = false)
    private UserService userService;

    @PostMapping(value = "/login")
    @ResponseBody
    public int login(HttpServletRequest request)  {
        int cc=0;
        String tel = request.getParameter("tel");
        String password = request.getParameter("password");
        List<User> list = userService.login1(tel);

        for (User user: list
             ) {
            if (user.getPassword().equals(password)) {
                HttpSession session = request.getSession();
                session.setAttribute("user", user);
                cc = 1;
            } else {
                cc = 0;
            }
        }
        return cc;
    }

//    @PostMapping(value = "/register")
//    @ResponseBody
//    public int regiser(HttpServletRequest request){
//        String tel = request.getParameter("tel");
//        String password = request.getParameter("password");
//        String email = request.getParameter("email");
//        String invitation = request.getParameter("invitation");
//        User user = new User();
//        user.setInvitation(invitation);
//        user.setPassword(password);
//        user.setTel(tel);
//        user.setEmail(email);
//        int register = userService.register(user);
//        return register;
//    }
    @RequestMapping(value = "/myinfo")
    @ResponseBody
    public Msg<User> myinfo(HttpServletRequest request){
        Msg<User> msg = new Msg<User>();
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        int id = user.getId();
        User user1 = userService.selectOne(id);
        session.setAttribute("user",user1);
        msg.setRes(user1);
        msg.setCode(1);
        return msg;
    }

    @PostMapping(value = "/update")
    @ResponseBody
    public Msg update(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        int id = user.getId();
        String tel = request.getParameter("tel");
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        user.setId(id);
        user.setEmail(email);
        user.setPassword(password);
        user.setTel(tel);
        System.out.println("tel = " + tel);
        int update = userService.update(user);
        Msg msg = new Msg();
        if(update==1){
            msg.setCode(1);
            msg.setMsg("修改成功");
        }else{
            msg.setCode(0);
            msg.setMsg("修改失败");
        }
        return msg;
    }

}
